package com.efounder.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;

import com.utilcode.util.Utils;

import java.io.IOException;
import java.util.Properties;

import skin.support.content.res.SkinCompatResources;

/**
 * 统一获取颜色值以及resourse资源
 *
 * @author YQS
 */
public class JfResourceUtil {

    private static Boolean isSupportSkin;

    //获取应用是否支持换肤
    public  static boolean getIsSupportSkin() {
        if (isSupportSkin == null) {
            Properties properties = new Properties();
            try {
                properties.load(Utils.getApp().getAssets().open("setting.properties"));
                String supportSkin = properties.getProperty("supportSkin", "0");
                isSupportSkin = supportSkin.equals("1");
            } catch (IOException e) {
                e.printStackTrace();
                isSupportSkin = false;
            }
        }
        return isSupportSkin;
    }

    /**
     * 获取换肤的指定颜色
     * 使用这种方式获取的颜色值。换肤框架才可以用
     *
     * @param color SkinCompatResodurces.getInstance().getColor
     * @return
     */
    public static int getSkinColor(@ColorRes int color) {
        if (getIsSupportSkin()) {
            return SkinCompatResources.getInstance().getColor(color);
        } else {
            return ContextCompat.getColor(Utils.getApp(), color);
        }

    }

    public static ColorStateList getSkinColorStateList(int color) {
        if (getIsSupportSkin()) {
            return SkinCompatResources.getInstance().getColorStateList(color);
        } else {
            return ContextCompat.getColorStateList(Utils.getApp(), color);
        }

    }

    /**
     * 获取换肤的指定drawable
     *
     * @param drawable
     * @return
     */
    public static Drawable getSkinDrawable(@DrawableRes int drawable) {
        if (getIsSupportSkin()) {
            return SkinCompatResources.getInstance().getDrawable(drawable);
        } else {
            return ContextCompat.getDrawable(Utils.getApp(), drawable);
        }
    }

    /**
     * 正常方法指定颜色
     *
     * @param color
     * @return
     */
    public static int getColor(Context context, @ColorRes int color) {
        return ContextCompat.getColor(context, color);
    }
}
